package main

import (
	"fmt"
	"time"
)

func main() {

	// var sequence int
	// var startTime time.Time
	// var endTime time.Time
	// startTimeStr:="2019-11-04 00:00:00"
	// endTimeStr:="2019-11-30 00:00:00"
	// startTime,err:=time.Parse("2006-01-02 15:04:05",startTimeStr)

	// if err==nil{
	//   fmt.Printf("%v",err) //打印结果：2017-04-11 13:33:37 +0000 UTC
	// }

	// endTime,endTimeErr:=time.Parse("2006-01-02 15:04:05",endTimeStr)

	// if endTimeErr==nil{
	//   fmt.Println(endTime) //打印结果：2017-04-11 13:33:37 +0000 UTC
	// }

	// sequence=GetWeekSequence(startTime,endTime,time.Now())

	// println("第",sequence,"周")

	loadLocation, _ := time.LoadLocation("Asia/Shanghai")
	nowTime, _ := time.ParseInLocation("2006-01-02", "2020-01-13", loadLocation)

	fmt.Println(WeekByDate(nowTime))
}

func WeekByDate(t time.Time) string {
	yearDay := t.YearDay()
	yearFirstDay := t.AddDate(0, 0, -yearDay+1)
	firstDayInWeek := int(yearFirstDay.Weekday())

	//今年第一周有几天
	firstWeekDays := 1
	if firstDayInWeek != 0 {
		firstWeekDays = 7 - firstDayInWeek + 1
	}
	var week int
	if yearDay <= firstWeekDays {
		week =  1
	} else {
		week = (yearDay-firstWeekDays)/7 + 2
	}
	return fmt.Sprintf("%d第%d周", t.Year(), week)
}

//获取当前时间在时间段内的第几周
//思路：首先用根据第一周的结束时间，判断时间段一共多少周。
// 一周，两周直接累加。三周以上将时间段分成所有三段，第一周，中间所有周，最后一周。并转成时间戳。（用于比较来判断第几周）
func GetWeekSequence(startTime time.Time, endTime time.Time, nowTime time.Time) int {

	weekSequence := 0

	timeLayout := "2006-01-02 15:04:05"
	planBegin := startTime.Unix()
	//第一周的开始时间和结束时间（时间戳） 结束时间：%v,firstWeekEndDay.Format("2006-01-02 15:04:05"),
	weekNumber := int(startTime.Weekday())
	if weekNumber == 0 { //周日，转成7
		weekNumber = 7
	}

	firstWeekEndDay := planBegin + int64(7-weekNumber+1)*3600*24
	if startTime.Weekday() == 0 { //周日，是0
		firstWeekEndDay = planBegin + 3600*24
	}

	println("计划开始时周几：", startTime.Weekday(), int(startTime.Weekday()))
	fmt.Printf("第一周开始时间:%v，时间戳：%v,结束时间戳：%v", startTime, planBegin, firstWeekEndDay)

	//最后一周的开始时间和结束时间（时间戳）
	planEnd := endTime.Unix()
	lastWeekDays := int(endTime.Weekday()) - 1
	//if int(plan.DateEnd.Weekday()) == 0 {
	//  lastWeekDays = 6
	//}
	if int(endTime.Weekday()) == 0 {
		lastWeekDays = 7
	}
	lastWeekEndDay := planEnd - int64(lastWeekDays*3600*24)
	println("")
	fmt.Printf("最后一周开始时间戳：%v,最后一周结束时间戳:%v", lastWeekEndDay, planEnd)

	//每周的结束时间

	//总周数
	Weeks := (lastWeekEndDay-firstWeekEndDay)/604800 + 2
	fmt.Printf("取整 int:%d", (lastWeekEndDay-firstWeekEndDay)/604800)
	floWeeks := float32(lastWeekEndDay-firstWeekEndDay) / 604800
	fmt.Printf("floweeks: %f", floWeeks)
	if floWeeks <= 1 {
		Weeks = 1
	}
	fmt.Printf("中间周数:%v,取余：%v", Weeks, (lastWeekEndDay-firstWeekEndDay)%604800)

	weekUnixs := make([]int, 0)
	weekUnixs = append(weekUnixs, int(planBegin))

	//分成三种情况，一周，两周，三周和三周以上
	//一周：firstWeekEndDay==lastWeekEndDay

	//两周:secondWeekEndDay==lastWeekEndDay
	println("")
	//planWeeks := 0
	println("第一周结束时间戳：", firstWeekEndDay, "form:", UnixToFormTime(firstWeekEndDay), "最后一周结束时间戳：", planEnd, "form:", UnixToFormTime(planEnd))
	if firstWeekEndDay >= planEnd { //一周
		weekUnixs = append(weekUnixs, int(planEnd))
		//planWeeks = 1
	} else if (firstWeekEndDay + 604800) >= planEnd { //二周
		weekUnixs = append(weekUnixs, int(firstWeekEndDay))
		weekUnixs = append(weekUnixs, int(planEnd))
		//planWeeks = 2
	} else { //三周和三周以上
		weekUnixs = append(weekUnixs, int(firstWeekEndDay))
		//planWeeks = 3
		println("开始,长度：", len(weekUnixs))
		for index, value := range weekUnixs {
			println("index:", index, "value:", value, time.Unix(int64(value), 0).Format(timeLayout))
		}

		println("---------")

		println("中间开始第一周第一天：", time.Unix(int64(firstWeekEndDay), 0).Format(timeLayout), "最后一周最后一天", time.Unix(int64(lastWeekEndDay), 0).Format(timeLayout), "周数：", int((lastWeekEndDay-firstWeekEndDay)/604800))

		lastWeekStartDay := lastWeekEndDay + 1
		for i := 0; i < int((lastWeekStartDay-firstWeekEndDay)/604800); i++ {
			weekUnixs = append(weekUnixs, int(firstWeekEndDay)+604800*(i+1))
		}
		println("---------")
		println("中间,长度：", len(weekUnixs))
		for index, value := range weekUnixs {
			println("index:", index, "value:", value, value, time.Unix(int64(value), 0).Format(timeLayout))
		}

		println("---------")
		weekUnixs = append(weekUnixs, int(planEnd))
		println("最后,长度：", len(weekUnixs))

		for index, value := range weekUnixs {
			println("index:", index, "value:", value, value, time.Unix(int64(value), 0).Format(timeLayout))
		}
	}


	for i := 0; i < len(weekUnixs); i++ {
		if int(nowTime.Unix()) >= weekUnixs[i] && int(nowTime.Unix()) <= weekUnixs[i+1] {
			weekSequence = i + 1
		}
	}
	println("本计划的周数：", weekSequence)
	return weekSequence
}

func UnixToFormTime(timeStamp int64) string {

	t := time.Unix(int64(timeStamp), 0)
	//返回string
	dateStr := t.Format("2006/01/02 15:04:05")
	return dateStr
}

